class Artist < ActiveRecord::Base
  belongs_to :region
  has_many :artworks, :conditions => 'artworks.published = true'
  has_many :medias
  has_many :artist_descriptions
  
  validates_presence_of :name
  validates_presence_of :region_id
  validates_uniqueness_of :name, :scope => :region_id
  
  acts_as_ferret :fields => [:name]
  
  def fullname
    name
  end
  
  def self.published_count 
    find_all_by_published(true).length
  end
  
  def to_param
    id.to_s + '-' + CGI.escape(fullname.downcase.gsub(' ', '-').gsub('.', ''))
  end
  
  include AdminHelper
  def unpublish_if_necessary
    check_requirements :artist, self
    if self.published && !meets_requirements?
      self.published = false
      self.save
      true
    else
      false
    end
  end
end
